package cn.edu.scnu.service;

import cn.edu.scnu.entity.Cart;
import cn.edu.scnu.entity.Flower;
import cn.edu.scnu.mapper.CartMapper;
import cn.edu.scnu.mapper.FlowerMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Flow;
@Service
public class CartService extends ServiceImpl<CartMapper, Cart> {
    private CartMapper cartMapper;
    private FlowerMapper flowerMapper;
    public CartService(CartMapper cartmapper , FlowerMapper flowerMapper){
        this.cartMapper = cartmapper;
        this.flowerMapper =  flowerMapper;
    }
    public void addcart(String email, String flowerid) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        queryWrapper.eq("flowerid", flowerid);
        Cart cart = cartMapper.selectOne(queryWrapper);
        if (cart != null) {
            cart.setNum(cart.getNum() + 1);
            cartMapper.updateById(cart);
        } else {
            Flower flower = flowerMapper.selectById(flowerid);
            Cart _cart = new Cart();
            _cart.setFlowerid(flowerid);
            _cart.setEmail(email);
            _cart.setFname(flower.getFname());
            _cart.setPictures(flower.getPictures());
            _cart.setPrice(flower.getPrice());
            _cart.setYourprice(flower.getYourprice());
            _cart.setNum(1);
            cartMapper.insert(_cart);
        }
    }

    public List<Cart> showcart(String email) {
        QueryWrapper<Cart>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("email",email);
        return cartMapper.selectList(queryWrapper);
    }

    public void updateCart(Integer cartId, Integer num) {
        Cart cart=cartMapper.selectById(cartId);
        cart.setNum(num);
        cartMapper.updateById(cart);
    }
}
